import pygame, sys, math

class Player(pygame.sprite.Sprite):
    def __init__(self, rsc, speed = [2,2], size = [100,100], pos = (0,0), living = True):
        #pygame.sprite.Sprite.__init__(self, self.containers)
        self.rsc = []
        self.size = size
        for image in rsc:
            newimage = pygame.image.load(image)
            newimage = pygame.transform.scale(newimage, self.size)
            self.rsc += [newimage]
        self.frame = 0
        self.maxFrame = len(self.rsc)-1
        self.waitCount = 0
        self.waitMax = 10
        self.image = self.rsc[self.frame]
        self.rect = self.image.get_rect()
        self.maxSpeedx = speed[0]
        self.maxSpeedy = speed[1]
        self.normalMaxSpeedx = speed[0]
        self.normalMaxSpeedy = speed[1]
        self.speedx = 0
        self.speedy = 0
        self.speed = [self.speedx, self.speedy]
        self.radius = self.rect.width/2
        self.place(pos)
        self.speedUpTimer = 0
        self.slowDownTimer = 0
        self.sizeUpTimer = 0
        self.health = 100
        self.healthTimer = 0
        self.invincible = False
        
    def place(self, pos):
        self.rect.center = pos
        
    def die(self):
        self.health = 0
        
    def life(self):
        self.health = 100
        
    def god(self):
        self.health = 100
        self.healthTimer = 60 *3
        self.invincible = True
        self.maxSpeedx = self.maxSpeedx*2
        self.maxSpeedy = self.maxSpeedy*2
        self.speedUpTimer = 180
        self.image = pygame.transform.scale(self.image, [self.size[0]*2, self.size[1]*2])
        self.rect = self.image.get_rect(center = self.rect.center)
        self.sizeUpTimer = 180
        
        
        
    def direction(self, dir):
        if dir == "right":
            self.speedx = self.maxSpeedx
        if dir == "stop right":
            self.speedx = 0
        if dir == "left":
            self.speedx = -self.maxSpeedx
        if dir == "stop left":
            self.speedx = 0
        if dir == "up":
            self.speedy = -self.maxSpeedy
        if dir == "stop up":
            self.speedy = 0
        if dir == "down":
            self.speedy = self.maxSpeedy
        if dir == "stop down":
            self.speedy = 0
    
    def update(self):
        self.move()
        if self.health <= 0:
            self.living = False
        #self.animate()
        if self.speedUpTimer > 1:
            self.speedUpTimer -= 1
        elif self.speedUpTimer == 1:
            self.speedUpTimer -= 1
            self.maxSpeedx = self.normalMaxSpeedx
            self.maxSpeedy = self.normalMaxSpeedy
        
        if self.sizeUpTimer > 1:
            self.sizeUpTimer -= 1
        elif self.sizeUpTimer == 1:
            self.sizeUpTimer -= 1
            self.image = pygame.transform.scale(self.image, self.size)
            self.rect = self.image.get_rect(center = self.rect.center)
    
        if self.slowDownTimer > 1:
            self.slowDownTimer -= 1
        elif self.slowDownTimer == 1:
            self.slowDownTimer -= 1
            self.maxSpeedx = self.normalMaxSpeedx
            self.maxSpeedy = self.normalMaxSpeedy
            
        if self.healthTimer > 1:
            self.healthTimer -= 1
        elif self.healthTimer == 1:
            self.invincible = False
            self.healthTimer -= 1
        
    def animate(self):
        if self.waitCount < self.waitMax:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if self.frame < self.maxFrame:
                self.frame += 1
            else:
                self.frame = 0
            self.image = self.rsc[self.frame]
        
    
    def move(self):
        self.speed = [self.speedx, self.speedy]
        self.rect = self.rect.move(self.speed)
        
    def collideWall(self, width, height):
        if self.rect.left < -(self.rect.width):
            self.place([width + self.rect.width/2, self.rect.center[1]])
        elif self.rect.right > width + self.rect.width:
            self.place([-self.rect.width/2, self.rect.center[1]])
        if self.rect.top < -(self.rect.height):
            self.place([self.rect.center[0], self.rect.height/2 + height])
        elif self.rect.bottom > height + self.rect.height:
            self.place([self.rect.center[0], -self.rect.height/2])
            
    def collideEnemy(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    if not self.invincible:
                        self.health -= other.damage
                        self.healthTimer = 60 * 1
                        self.invincible = True
                    
            
    def collideAstroid(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    other.living = False
                    if self.rect.center[0] < other.rect.center[0]: #self left of other
                        if other.speedx < 0: #moving left
                            other.speedx = -other.speedx
                    if self.rect.center[0] > other.rect.center[0]: #self right of other
                        if other.speedx > 0: #moving right
                            other.speedx = -other.speedx
                    if self.rect.center[1] < other.rect.center[1]: #self above other
                        if other.speedy < 0: #moving up
                            other.speedy = -other.speedy
                    if self.rect.center[1] > other.rect.center[1]:#self below other
                        if other.speedy > 0: #moving down
                            other.speedy = -other.speedy
                            
    def collideBlock(self, block):
        #print self.rect, self.headingx, self.headingy
        if self.floor == block.rect.top + 2 and self.headingy == "none":
            self.touchFloor = True
            self.jumping = False
        else:
            if self.realx < block.realx and self.headingx == "right":
                self.speedx = 0
                self.realx -= 1
                self.x -= 1
                #print "hit right"
            if self.realx > block.realx and self.headingx == "left":
                self.speedx = 0
                self.realx += 1
                self.x += 1
                #print "hit left"
            if self.realy < block.realy and self.headingy == "up":
                self.speedy = 0
                self.realy += 1
                self.y += 1
                #print "hit up"
            if self.realy > block.realy and self.headingy == "down":
                self.touchFloor = True
                self.speedy = 0
                self.realy -= self.g + 2
                self.headingy = "none"
                self.floor = block.rect.top+2
                self.y = self.floor - self.rect.height/2
                
                            
    def collidePU(self, other):
        if self.rect.right > other.rect.left and self.rect.left < other.rect.right:
            if self.rect.bottom > other.rect.top and self.rect.top < other.rect.bottom:
                if self.radius + other.radius > self.distanceToPoint(other.rect.center):
                    if other.type == "Speed":
                        self.speedx += 3
                        self.speedy += 3
                    if other.type == "Aura":
                        self.size += [50,50]
                    if other.type == "Slow":
                        self.speedx += 3
                        self.speedy += 3
                    if other.type == "Instant-Death":
                        self.health = 0 
                        
    def collideMoney(self, money):
        self.money += money.value
        
                        
    def speedUp(self):
        self.maxSpeedx = self.maxSpeedx*2
        self.maxSpeedy = self.maxSpeedy*2
        self.speedUpTimer = 180
    
    def slowDown(self):
        self.maxSpeedx = self.maxSpeedx/2
        self.maxSpeedy = self.maxSpeedy/2
        self.slowDownTimer = 180
        
    def sizeUp(self):
        self.image = pygame.transform.scale(self.image, [self.size[0]*2, self.size[1]*2])
        self.rect = self.image.get_rect(center = self.rect.center)
        self.sizeUpTimer = 180
    
    def distanceToPoint(self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[1]
        x2 = pt[0]
        y2 = pt[1]
        
